package org.jvending.provisioning.portal;

import org.jvending.provisioning.xml.discovery.ObjectFactory;
import org.jvending.provisioning.xml.discovery.BundlesType;
import org.jvending.provisioning.xml.discovery.Bundles;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.JAXBException;
import javax.provisioning.ProvisioningContext;
import javax.provisioning.BundleDescriptor;
import javax.provisioning.Deliverable;
import java.util.logging.Logger;
import java.util.Collection;
import java.util.List;
import java.io.OutputStream;
import java.io.IOException;

public class AndroidDiscoveryServlet extends HttpServlet {

    private static Logger logger = Logger.getAnonymousLogger();

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String serverURI = this.getServletContext().getInitParameter("CONTENT_DELIVERY_URI");

        ProvisioningContext provisioningContext = (ProvisioningContext)
                this.getServletContext().getAttribute("javax.provisioning.ProvisioningContext");
        Collection<BundleDescriptor> bundleDescriptors =
                provisioningContext.getBundleRepository().getBundlesFor(null, null);
        OutputStream os = response.getOutputStream();
        response.setContentType("text/xml");

        ObjectFactory factory = new ObjectFactory();
        try {
            Bundles bundles = factory.createBundles();
            for (BundleDescriptor bundleDescriptor : bundleDescriptors) {
                BundlesType.Bundle bundle = factory.createBundlesTypeBundle();
                bundle.setDisplayName(bundleDescriptor.getDisplayName(null));
                bundle.setShortDescription(bundleDescriptor.getDescription(null));
                bundle.setBundleId(bundleDescriptor.getBundleID());
                bundle.setTermsAndConditions(bundleDescriptor.getCopyright(null));
                bundle.setVendorName(bundleDescriptor.getVendor(null));
                List<Deliverable> icons = bundleDescriptor.getIcons(null);
                if (!icons.isEmpty()) {
                    bundle.setIcon(icons.get(0).getURL().toString());
                }
                bundle.setContentURI(serverURI + "/AdapterController?bundleId=" + bundleDescriptor.getBundleID());
                bundles.getBundle().add(bundle);
            }

            JAXBContext jaxbContext = JAXBContext.newInstance("org.jvending.provisioning.xml.discovery");
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.marshal(bundles, os);
        } catch (JAXBException e) {
            throw new IOException(e.getMessage());
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

    public final void writeContents(OutputStream os) throws IOException {

    }
}
